
package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 13-9-9
* Time: 下午12:56
* To change this template use File | Settings | File Templates.
*/
@SuppressWarnings("serial")
@TableBind(tableName="point_rule",pkName = "rule_id")
public class PointRule extends BasicModel<PointRule> {
    public static final PointRule dao = new PointRule();
    public static final String SELECT_ALL = "`rule_id`,`name`,`action_code`,`create_time`,`start_time`,`expire_time`,`point_num`,`user_level`,`is_repeat`,`describe`,`status`,`is_continuous`";


    public PointRule findByActionCode(String actionCode){
        return findFirst("select * from point_rule where action_code = ?",actionCode);
    }

    public List<PointRule> findPointRule(PointRule pointRule){
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("SELECT point_rule.* FROM point_rule where 1=1");
        if (pointRule != null) {
            String[] attrNames = pointRule.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = pointRule.get(attrName);
                if (attrValue != null) {
                    if("start_time".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND point_rule.`start_time` < DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("expire_time".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND point_rule.`expire_time` > DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
//            sqlExceptSelectBuffer.append(" order by rule_id desc");

        }
        return find(sqlExceptSelectBuffer.toString(),paras.toArray());
    }


    public Page<PointRule> paginate(Integer pageNumber, Integer pageSize, PointRule order) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM point_rule ");

        StringBuffer selectBuffer = new StringBuffer("SELECT point_rule.* ");

        sqlExceptSelectBuffer.append(" where 1=1 ");
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        }else{
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (order != null) {
            String[] attrNames = order.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = order.get(attrName);
                if (attrValue != null) {
                    System.out.println("attrvalue="+attrName);
                    if("action_code".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND action_code LIKE ? ");
                        attrValue = "%"+attrValue+"%";
                    }else if("name".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND name LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }

        Page<PointRule> orderPage;
        if (paras.size() > 0){
            orderPage = paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }else{
            orderPage = paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
        }
        return orderPage;
    }

    public PointRule setRuleId (Long ruleId){
        this.set("rule_id",ruleId);
        return this;
    }

    public Long getRuleId (){
        return this.get("rule_id");
    }

    public PointRule setName (String name){
        this.set("name",name);
        return this;
    }

    public String getName (){
        return this.getStr("name");
    }

    public PointRule setActionCode (String actionCode){
        this.set("action_code",actionCode);
        return this;
    }

    public String getActionCode (){
        return this.getStr("action_code");
    }

    public PointRule setCreateTime (Date createTime){
        this.set("create_time",createTime);
        return this;
    }

    public Date getCreateTime (){
        return this.getDate("create_time");
    }

    public PointRule setExpireTime (Date expireTime){
        this.set("expire_time",expireTime);
        return this;
    }

    public Date getExpireTime (){
        return this.getTimestamp("expire_time");
    }

    public PointRule setStartTime (Date startTime){
        this.set("start_time",startTime);
        return this;
    }

    public Date getStartTime (){
        return this.getTimestamp("start_time");
    }

    public PointRule setPointNum (Integer pointNum){
        this.set("point_num",pointNum);
        return this;
    }

    public Integer getPointNum (){
        return this.get("point_num");
    }

    public PointRule setUserLevel (Integer userLevel){
        this.set("user_level",userLevel);
        return this;
    }

    public Integer getUserLevel (){
        return this.get("user_level");
    }

    public PointRule setIsRepeat (Integer isRepeat){
        this.set("is_repeat",isRepeat);
        return this;
    }

    public Integer getIsRepeat (){
        return this.get("is_repeat");
    }

    public PointRule setDescribe (String describe){
        this.set("describe",describe);
        return this;
    }

    public String getDescribe (){
        return this.getStr("describe");
    }

    public PointRule setStatus (Integer status){
        this.set("status",status);
        return this;
    }

    public Integer getStatus (){
        return this.get("status");
    }

    public PointRule setActionCount (Integer count){
        this.set("action_count",count);
        return this;
    }

    public Integer getActionCount (){
        return this.get("action_count");
    }

    public PointRule setMaxNum (Integer num){
        this.set("max_num",num);
        return this;
    }

    public Integer getMaxNum (){
        return this.get("max_num");
    }

    public PointRule setRepeatInterval (Integer interval){
        this.set("repeat_interval",interval);
        return this;
    }

    public Integer getRepeatInterval (){
        return this.get("repeat_interval");
    }

    public PointRule setIsContinuous(Boolean isContinuous){
        return this.set("is_continuous", isContinuous);
    }

    public Boolean getIsContinuous(){
        return this.getBoolean("is_continuous");
    }
}
